﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Century.Common.Business;
using Century.Common.Business.BOL.Common;
using Telerik.Web.Mvc;
using Century.Models.Common;
using System.Collections;
using Century.Web.Security.Attributes;
using Century.Web.UI.MVC;
using Century.Business.Repository;

namespace Century.Admin.Controllers
{
    [Authorization(Group = "", Name = "Glossary")]
    public class GlossaryController : CenturyAdminBaseController
    {
        GlossaryBSO db = new GlossaryBSO();
        CountrySiteBSO countryDb = new CountrySiteBSO();

        public ActionResult Index()
        {
            GetCommonForeignKeys();
            return View();
        }

        [GridAction]
        public ActionResult GetGlossaryList()
        {
            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new Glossary { Id = c.Id, Definition = c.Definition, Title = c.Title, CountrySiteId = c.CountrySiteId }).OrderByDescending(c => c.Id)
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditGlossary(int id)
        {
            string ownedSitePathes = PersistData.CurrentSite.Path;
            Glossary glossary = db.Find(p => p.Id == id);
            TryUpdateModel(glossary);

            db.Update(glossary);

            var d = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new Glossary { Id = c.Id, Definition = c.Definition, Title = c.Title, CountrySiteId = c.CountrySiteId }).OrderByDescending(c => c.Id)
            };
            return View(d);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertGlossary(Glossary mdl)
        {

            string ownedSitePathes = PersistData.CurrentSite.Path;
            if (ModelState.IsValid)
            {
                //The model is valid - insert the product.
                db.Insert(mdl);
            }

            var d = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new Glossary { Id = c.Id, Definition = c.Definition, Title = c.Title, CountrySiteId = c.CountrySiteId }).OrderByDescending(c => c.Id)
            };

            return View(d);
        }

        [GridAction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteGlossary(int id)
        {
            Glossary glossary = new Glossary();
            glossary = db.Find(c => c.Id == id);
            db.Delete(glossary);

            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new Glossary { Id = c.Id, Definition = c.Definition, Title = c.Title, CountrySiteId = c.CountrySiteId }).OrderBy(c => c.Id)
            };

            GetCommonForeignKeys();

            return View(model);
        }

        public void GetCommonForeignKeys()
        {
            ViewBag.CountrySitePath = Token.AccessibleSites.Select(c => new { c.Id, c.Path }).OrderBy(c => c.Path).AsEnumerable();
        }

    }
}

